package microBlogger;

import context.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;

import javax.microedition.rms.RecordStore;

public class AccountRecordStore implements Debug.Constants
{ //private static final boolean DEBUG=true; // Overriding Debug.Constants.DEBUG
  private static final String ACCOUNT_RECORD_STORE = "BloggerAccount"; // Recordstore
  public final static int MAX_NICK=32;
  public final static int MAX_USERID=32;
  public final static int MAX_PASSWORD=32;
  private String nickName;
  private String userID;
  private String password;
  
  public AccountRecordStore()
  {
	  readAccountStore();
  }
  
  public String getNickName() { return(nickName); }
  public String getUserID() { return(userID); }
  public String getPassword() { return(password); }
  
  public void update(String nickName,String userID,String password)
  { this.nickName=nickName;
    this.userID=userID;
    this.password=password;
	if(DEBUG) Debug.println("AccountRecordStore.update: "+this);
    this.store();
  }

  private void store()
  { RecordStore rs = null;
    try { RecordStore.deleteRecordStore(ACCOUNT_RECORD_STORE); }
    catch( Exception e ){ /* TODO status */ }
    try
    { rs = openRecordStore(ACCOUNT_RECORD_STORE);
      byte[] record;
      ByteArrayOutputStream bout = new ByteArrayOutputStream();
      DataOutputStream dout = new DataOutputStream( bout );
        dout.writeUTF(nickName);
        dout.writeUTF(userID);
        dout.writeUTF(password);
        record = bout.toByteArray();
        rs.addRecord(record, 0, record.length);
      dout.close();
      bout.close();
    }
    catch(Exception e) { Debug.EXCEPTION("AccountRecordStore.store",e); }
    finally { closeRecordStore(rs); }
  }

  private void readAccountStore()
  { byte[] rsData = new byte[MAX_NICK+2+MAX_USERID+2+MAX_PASSWORD+2];
    RecordStore rs = null;
    try
    { rs = openRecordStore(ACCOUNT_RECORD_STORE);
      if (rs.getNumRecords() < 1)
      { System.err.println("no records"); return;
      }
      ByteArrayInputStream bin = new ByteArrayInputStream(rsData);
      DataInputStream din = new DataInputStream(bin);
    	rs.getRecord(1, rsData, 0);
        nickName=din.readUTF();
        userID=din.readUTF();
        password=din.readUTF();
      bin.close();
      din.close();
      //System.err.println("read ok");
    }
    catch (Exception e) { Debug.EXCEPTION("AccountRecordStore.readAccountStore",e); }
    finally { closeRecordStore(rs); }
  }
		
  private RecordStore openRecordStore(String name)
  { try { return RecordStore.openRecordStore(name, true); }
    catch (Exception e)
    { System.err.println(e.toString());
      return null;
    }
  }

  private void closeRecordStore(RecordStore rs)
  { try { rs.closeRecordStore(); }
    catch (Exception e) { System.err.println(e.toString());
    }
  }

  public String toString()
  { if(DEBUG) return("AccountRecordStore[nickName="+nickName+",userID="+userID+",password="+password+']');
    else return(super.toString());
  }

}
